package cn.edu.lingnan.controller;


import cn.edu.lingnan.entity.dto.BrandDto;
import cn.edu.lingnan.service.BrandService;
import cn.edu.lingnan.entity.pojo.Brand;
import cn.edu.lingnan.entity.pojo.Result;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Date;

@RestController
@RequestMapping("/v1/brand-service")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @PostMapping("/brands")
    public Result<Page<Brand>> getBrands(@RequestBody BrandDto brandDto) {
        return Result.success(brandService.getBrandsByPage(brandDto));
    }

    //根据id查询品牌
    @GetMapping("/brandById/{brandId}")
    public Result<Brand> getBrandById(@PathVariable("brandId") Integer brandId) {
        Brand brand = brandService.getById(brandId);
        return Result.success(brand);
    }

    //删除品牌
    @DeleteMapping("/brand")
    public Result deleteBrand(@RequestParam("brandId") Integer brandId) {
         brandService.removeById(brandId);
         return Result.success();

    }
    @PutMapping("/brand")
    public Result updateBrand(@RequestParam("brandId") Integer brandId, @RequestParam("status") Integer status) {
        // 使用 LambdaUpdateWrapper 构建更新条件
        boolean isUpdated = brandService.update(new LambdaUpdateWrapper<Brand>()
                .eq(Brand::getBrandId, brandId) // 条件：brandId 匹配
                .set(Brand::getStatus, status)  // 设置新的状态值
        );

        // 判断是否更新成功
        if (isUpdated) {
            return Result.success("品牌状态更新成功");
        } else {
            return Result.error("品牌状态更新失败");
        }
    }

    // 注入文件上传路径，可以在application.properties中配置

    private String uploadDir="E:/study/大三下期末/软件工程/代码/cloud-parent/cloud-service/user-service/src/main/resources/static/logos";

    @PostMapping("/add-brand")
    public Result addBrand(@RequestParam("brandName") String brandName,
                           @RequestParam("brandDesc") String brandDesc,
                           @RequestParam("status") Integer status,
                           @RequestParam("logoFile") MultipartFile logoFile) {
        if (logoFile.isEmpty()) {
            return Result.error("Logo文件不能为空");
        }

        // 获取文件名并构建文件保存路径
        String originalFilename = logoFile.getOriginalFilename();
        String fileExtension = "";

        if (originalFilename != null && originalFilename.contains(".")) {
            fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        }

        String newFilename = System.currentTimeMillis() + fileExtension; // 生成新的文件名，避免重复
        String filePath = Paths.get(uploadDir, newFilename).toString();

        File dest = new File(filePath);
        try {
            // 确保目录存在
            dest.getParentFile().mkdirs();
            // 保存文件
            logoFile.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("Logo上传失败");
        }

        // 构建品牌对象
        Brand brand = new Brand();
        brand.setBrandName(brandName);
        brand.setBrandDesc(brandDesc);
        brand.setStatus(status);
        brand.setLogoUrl(filePath); // 存储文件的绝对路径
        brand.setCreateTime(new Date());
        brand.setUpdateTime(new Date());

        // 保存品牌到数据库
        boolean isSaved = brandService.save(brand);

        if (isSaved) {
            return Result.success("品牌添加成功");
        } else {
            return Result.error("品牌添加失败");
        }
    }


}
